package demo.spring.da.jdbc;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by lushun.chen on 2017/3/25.
 *
 * @author lushun.chen
 */
public class JdbcCorporateEventDao implements CorporateEventDao {

    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public int count() {
        return this.jdbcTemplate.queryForObject("select count(*) from cart_line", Integer.class);
    }

    @Override
    public CartLine findCartline() {
        return jdbcTemplate.queryForObject("select * from cart_line where id =?",new Object[]{21L},new RowMapper<CartLine>(){
            @Override
            public CartLine mapRow(ResultSet resultSet, int i) throws SQLException {
                CartLine cartLine=new CartLine();
                cartLine.setId(resultSet.getLong("id"));
                cartLine.setAccountId(resultSet.getLong("user_account_id"));
                return cartLine;
            }
        });
    }
}
